package com.ymm.lulutong.utils;

import com.ymm.lulutong.domain.ShipperCargoDomainService;
import com.ymm.lulutong.domain.ShipperProfileDomainService;
import com.ymm.lulutong.entity.Address;
import com.ymm.lulutong.entity.Shipper;
import com.ymm.lulutong.mapper.ShipperAddressMapper;
import com.ymm.lulutong.repo.SystemDefinePriceServiceRepo;
import com.ymm.lulutong.repo.SystemShipperCargoServiceRepo;
import com.ymm.lulutong.request.CargoDefinePriceWebRequest;
import com.ymm.system.api.dto.PutOnCargoRequest;
import com.ymm.system.api.utils.CargoTypeEnum;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@SpringBootTest
@RunWith(SpringRunner.class)
public class AutoPutOnCargoRequest {
    @Resource
    SystemShipperCargoServiceRepo systemShipperCargoServiceRepo;

    @Resource
    SystemDefinePriceServiceRepo systemDefinePriceServiceRepo;

    @Resource
    ShipperProfileDomainService shipperProfileDomainService;

    @Resource
    ShipperAddressMapper shipperAddressMapper;

    Random random = new Random();

    @Test
    public void test() throws Exception {
        int number = 20;
        for (int i = 0; i < number; i++) {
            PutOnCargoRequest putOnCargoRequest = randomGenerateCargo();
//            System.out.println(putOnCargoRequest);
//            Result<Boolean> booleanResult = systemShipperCargoServiceRepo.cargoDeliver(putOnCargoRequest, putOnCargoRequest.getShipperId());
//            if (!booleanResult.isSuccess()) {
//                System.out.println(booleanResult);
//            }
        }
    }

    private PutOnCargoRequest randomGenerateCargo() {
        PutOnCargoRequest putOnCargoRequest = new PutOnCargoRequest();
        putOnCargoRequest.setShipperId(randomGenerateShipperId());
        Address srcAddress = randomGenerateAddress();
        Address desAddress = randomGenerateAddress();
        assert srcAddress != null;
        putOnCargoRequest.setCargoSrcProv(srcAddress.getAddressProv());
        putOnCargoRequest.setCargoSrcCity(srcAddress.getAddressCity());
        putOnCargoRequest.setCargoSrcRegion(srcAddress.getAddressRegion());
        putOnCargoRequest.setCargoSrcLat(srcAddress.getAddressLat());
        putOnCargoRequest.setCargoSrcLon(srcAddress.getAddressLon());
        putOnCargoRequest.setCargoSrc(srcAddress.getAddressSrc());

        assert desAddress != null;
        putOnCargoRequest.setCargoDesProv(desAddress.getAddressProv());
        putOnCargoRequest.setCargoDesCity(desAddress.getAddressCity());
        putOnCargoRequest.setCargoDesRegion(desAddress.getAddressRegion());
        putOnCargoRequest.setCargoDesLat(desAddress.getAddressLat());
        putOnCargoRequest.setCargoDesLon(desAddress.getAddressLon());
        putOnCargoRequest.setCargoDes(desAddress.getAddressSrc());

        putOnCargoRequest.setCargoPersonNum(0);
        putOnCargoRequest.setCargoWeight(randomGenerateCargoWeight());
        putOnCargoRequest.setCargoVolume(randomGenerateCargoVolume());
        putOnCargoRequest.setCargoDescription(randomGenerateDescription());
        putOnCargoRequest.setCargoComment(putOnCargoRequest.getCargoDescription());

        putOnCargoRequest.setCargoShipperPhone(randomGeneratePhone());
        putOnCargoRequest.setCargoReceiverPhone(randomGeneratePhone());

        putOnCargoRequest.setCargoRequireType(randomGenerateCargoRequireType());
        putOnCargoRequest.setCargoType(randomGenerateCargoType());
        if (putOnCargoRequest.getCargoType().equals(CargoTypeEnum.IMMEDIATELY.getCode())) {
            putOnCargoRequest.setCargoDeliveryTime(new Date());
        } else {
            putOnCargoRequest.setCargoDeliveryTime(randomGenerateCargoDeliveryTime());
        }
        CargoDefinePriceWebRequest cargoDefinePriceWebRequest = new CargoDefinePriceWebRequest();
        cargoDefinePriceWebRequest.setCity(putOnCargoRequest.getCargoSrcCity());
        cargoDefinePriceWebRequest.setCargoRequireType(putOnCargoRequest.getCargoRequireType());
        cargoDefinePriceWebRequest.setCargoDesLat(putOnCargoRequest.getCargoDesLat());
        cargoDefinePriceWebRequest.setCargoDesLon(putOnCargoRequest.getCargoDesLon());
        cargoDefinePriceWebRequest.setCargoSrcLat(putOnCargoRequest.getCargoSrcLat());
        cargoDefinePriceWebRequest.setCargoSrcLon(putOnCargoRequest.getCargoSrcLon());
        cargoDefinePriceWebRequest.setCargoDeliveryTime(putOnCargoRequest.getCargoDeliveryTime());
        Result<Integer> priceResult = systemDefinePriceServiceRepo.definePrice(cargoDefinePriceWebRequest);
        putOnCargoRequest.setCargoPrice(priceResult.getData());


        return putOnCargoRequest;
    }

    private Date randomGenerateCargoDeliveryTime() {
        Calendar cc = Calendar.getInstance();
        cc.add(Calendar.DATE, random.nextInt(3));
        cc.add(Calendar.HOUR, random.nextInt(10));
        return cc.getTime();
    }

    private Integer randomGenerateCargoType() {
        return random.nextInt(2);
    }

    private Integer randomGenerateCargoRequireType() {
        return random.nextInt(8) + 1;
    }

    private String randomGeneratePhone() {
        Long shipperId = randomGenerateShipperId();
        Shipper shipper = shipperProfileDomainService.getShipperByShipperId(shipperId).getData();
        while (shipper == null || shipper.getPhoneNumber().equals("1")) {
            shipperId = randomGenerateShipperId();
            shipper = shipperProfileDomainService.getShipperByShipperId(shipperId).getData();
            System.out.println("null phone");
        }
        return shipper.getPhoneNumber();
    }

    private String randomGenerateDescription() {
        List<String> cargoDescriptionList = new ArrayList<String>() {{
            add("苹果");
            add("香蕉");
            add("石灰");
            add("石头");
            add("卡车");
            add("桌子");
            add("杂物");
        }};
        int index = random.nextInt(cargoDescriptionList.size());
        return cargoDescriptionList.get(index);
    }

    private BigDecimal randomGenerateCargoVolume() {
        return BigDecimal.valueOf((double) random.nextInt(100) / 100);
    }

    private BigDecimal randomGenerateCargoWeight() {
        return BigDecimal.valueOf((double) random.nextInt(100) / 100);
    }

    private Address randomGenerateAddress() {
        long addressId = random.nextInt(12);
        Address address = shipperAddressMapper.getByAddressId(addressId);
        while (address == null) {
            addressId = random.nextInt(12);
            address = shipperAddressMapper.getByAddressId(addressId);
            System.out.println("null address " + addressId);
        }
        return address;
    }

    private Long randomGenerateShipperId() {
        Long shipperId = (long) random.nextInt(18);
        Shipper shipper = shipperProfileDomainService.getShipperByShipperId(shipperId).getData();
        while (shipper == null){
            shipperId = (long) random.nextInt(18);
            shipper = shipperProfileDomainService.getShipperByShipperId(shipperId).getData();
            System.out.println("null phone");
        }
        return shipperId;
    }
}
